package com.baiye.template.controller;

import com.baiye.template.common.R;
import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.Role;
import com.baiye.template.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 16:43
 */
@RestController
@Api(value = "角色管理")
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping(value = "/deleteRoleById",method = RequestMethod.GET)
    public R deleteRoleById(int id){
        roleService.deleteById(id);
        return new R().ok();
    }
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public R addRole(@RequestBody Role role){
        roleService.addRole(role);
        return new R().ok();
    }
    @RequestMapping(value = "/addRoleToUser",method = RequestMethod.GET)
    public R addRoleToUser(int userId, int roleId){
        roleService.addRoleToUser(userId, roleId);
        return new R().ok();
    }
    @RequestMapping(value = "/deleteRoleToUser",method = RequestMethod.GET)
    public R deleteRoleToUser(int userId, int roleId){
        roleService.deleteRoleToUser(userId, roleId);
        return new R().ok();
    }
    @RequestMapping(value = "/getRole",method = RequestMethod.POST)
    public R getRole(@RequestBody Role role, @Param(value = "pageIndex")Integer pageIndex, @Param(value = "pageSize") Integer pageSize){
        List<Role> i = roleService.getRole(role,pageIndex,pageSize);
        PageInfo<Role> pageInfo = new PageInfo<>(i);
        return new R().ok(pageInfo);
    }
    @RequestMapping(value = "/getRoleByUserId",method = RequestMethod.GET)
    public R getRoleByUserId(Integer userId){
        List<Role> i = roleService.getRoleByUserId(userId);
        return new R().ok(i);
    }
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    public R updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return new R().ok();
    }

}
